package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 
 * @author Mathias
 */
public class Order
{
    private int
            id;
    private boolean finished;
    private String
            comment,
            order_date,
            pickup_date,
            delivery_date,
            invoice_date;
    private double 
            payment_received;  
    
    private User user;
    private ProgramValue programValues;
    private TombStone tombStone;
    private CustomerSpecified[] customerSpecified;
    private ArrayList<OrderLine> orderLine;
    private ArrayList<ActionListener> listeners;

    public Order(int id, String comment, boolean finished, String order_date, String pickup_date, 
                    String delivery_date, String invoice_date, double payment_received, 
                    User user, ProgramValue programValues, TombStone tombStone, CustomerSpecified[] customerSpecified)
    {
        this.id = id;
        this.comment = comment;
        this.finished = finished;
        this.order_date = order_date;
        this.pickup_date = pickup_date;
        this.delivery_date = delivery_date;
        this.invoice_date = invoice_date;
        this.payment_received = payment_received;
        this.user = user;
        this.programValues = programValues;
        this.tombStone = tombStone;
        this.customerSpecified = customerSpecified;
        this.orderLine = new ArrayList();
        listeners = new ArrayList();
        notifyListeners();
    }

    public Order() throws SQLException, ClassNotFoundException
    {
        this.customerSpecified = new CustomerSpecified[2];
        customerSpecified[0] = new CustomerSpecified();
        customerSpecified[1] = new CustomerSpecified();
        this.orderLine = new ArrayList();
        listeners = new ArrayList();
        reset();
    }
    
    public void reset() throws SQLException, ClassNotFoundException
    {
        this.id = 0;
        this.comment = "";
        this.finished = false;
        this.order_date = "";
        this.pickup_date = "";
        this.delivery_date = "";
        this.invoice_date = "";
        this.payment_received = 0;
        this.user = null;
        this.programValues = model.handlers.ProgramValueHandler.getCurrentProgramValueModelObject();
        this.tombStone = null;
        this.customerSpecified[0].reset();
        this.customerSpecified[1].reset();
        this.orderLine.clear();
        listeners.clear();
        notifyListeners();
    }
    
    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
        notifyListeners();
    }

    public String getComment()
    {
        return comment;
    }

    public void setComment(String comment)
    {
        this.comment = comment;
        notifyListeners();
    }

    public boolean isFinished()
    {
        return finished;
    }

    public void setFinished(boolean finished)
    {
        this.finished = finished;
        notifyListeners();
    }

    public String getOrder_date()
    {
        return order_date;
    }

    public void setOrder_date(String order_date)
    {
        this.order_date = order_date;
        notifyListeners();
    }

    public String getPickup_date()
    {
        return pickup_date;
    }

    public void setPickup_date(String pickup_date)
    {
        this.pickup_date = pickup_date;
        notifyListeners();
    }

    public String getDelivery_date()
    {
        return delivery_date;
    }

    public void setDelivery_date(String delivery_date)
    {
        this.delivery_date = delivery_date;
        notifyListeners();
    }

    public String getInvoice_date()
    {
        return invoice_date;
    }

    public void setInvoice_date(String invoice_date)
    {
        this.invoice_date = invoice_date;
        notifyListeners();
    }

    public double getPayment_received()
    {
        return payment_received;
    }

    public void setPayment_received(double payment_received)
    {
        this.payment_received = payment_received;
        notifyListeners();
    }

    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
        notifyListeners();
    }

    public ProgramValue getProgramValues()
    {
        return programValues;
    }

    public void setProgramValues(ProgramValue programValues)
    {
        this.programValues = programValues;
        notifyListeners();
    }

    public TombStone getTombStone()
    {
        return tombStone;
    }

    public void setTombStone(TombStone tombStone)
    {
        this.tombStone = tombStone;
        notifyListeners();
    }

    public CustomerSpecified[] getCustomerSpecified()
    {
        return customerSpecified;
    }

    public void setCustomerSpecified(CustomerSpecified[] customerSpecified)
    {
        this.customerSpecified = customerSpecified;
        notifyListeners();
    }
    
    public void setCustomerSpecified(int i, CustomerSpecified customerSpecified)
    {
        this.customerSpecified[i] = customerSpecified;
        notifyListeners();
    }

    public ArrayList<OrderLine> getOrderLine()
    {
        return orderLine;
    }

    public void setOrderLine(ArrayList<OrderLine> orderLine)
    {
        this.orderLine = orderLine;
        notifyListeners();
    }
    
    public void addOrderLine(OrderLine line)
    {
        orderLine.add(line);
        notifyListeners();
    }
    
    public void removeOrderLine(OrderLine line)
    {
        orderLine.remove(line);
        notifyListeners();
    }
    
    public void addListener(ActionListener listener) 
    {
        listeners.add(listener);
        regListeners(listener);
    }
    
    private void regListeners(ActionListener listener)
    {
        customerSpecified[0].addListener(listener);
        customerSpecified[0].getCustomerName().addListener(listener);
        customerSpecified[0].getCustomerAddress().addListener(listener);
        customerSpecified[0].getCustomerTelephone().addListener(listener);        
        customerSpecified[1].addListener(listener);
        customerSpecified[1].getCustomerName().addListener(listener);
        customerSpecified[1].getCustomerAddress().addListener(listener);
        customerSpecified[1].getCustomerTelephone().addListener(listener);
        
        if (tombStone != null)
        {
            tombStone.addListener(listener);
        }
    }

    public void notifyListeners() 
    {
        ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Something changed");
        for (ActionListener listener : listeners) 
        {
            listener.actionPerformed(event);
        }
    }
    
    public double totalPrice()
    {
        double toReturn = 0;
        
        for (int i = 0; i < orderLine.size(); i++)
        {
            toReturn += (orderLine.get(i).getAmount() * orderLine.get(i).getUnitPrice());
        }
        
        return toReturn;
    }
    
    public double totalPriceWithEnvironment()
    {
        return totalPrice() * getEnvironmentMultiplier();
    }
    
    public double totalPriceVat()
    {
        return totalPriceWithEnvironment() * getVatMultiplier();
    }
    
    public double getEnvironmentMultiplier()
    {
        return (1 + (programValues.getEnvironment_tax() / 100));
    }
    
    public double getVatMultiplier()
    {
        return (1 + (programValues.getVat()/ 100));
    }
}